package com.chcit.share.security.token;

import com.chcit.share.security.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.Assert;
import com.chcit.share.security.token.filter.*;
/**
 *  token配置类，会自动合并到springsecurity的配置中
 */
//@Component
public class TokenSecurityAdapterConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SecurityProperties securityProperties;

    AuthenticationSuccessHandler authenticationSuccessHandler;

    AuthenticationFailureHandler authenticationFailureHandler;

    public TokenSecurityAdapterConfig setAuthenticationSuccessHandler(AuthenticationSuccessHandler successHandler) {
        Assert.notNull(successHandler, "successHandler cannot be null");
        authenticationSuccessHandler = successHandler;
        return this;
    }

    public TokenSecurityAdapterConfig setAuthenticationFailureHandler(AuthenticationFailureHandler failureHandler) {
        Assert.notNull(failureHandler, "failureHandler cannot be null");
        authenticationFailureHandler = failureHandler;
        return  this;
    }



    @Override
    public void configure(HttpSecurity http) throws Exception {
        ChcitTokenGeneratorFilter jwtTokenGeneratorFilter = new ChcitTokenGeneratorFilter(authenticationManager,securityProperties,userDetailsService);
        if(authenticationFailureHandler != null){
            jwtTokenGeneratorFilter.setAuthenticationFailureHandler(authenticationFailureHandler);
        }
        if(authenticationSuccessHandler != null){
            jwtTokenGeneratorFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler);
        }

        http.authenticationProvider(new JwtAuthenticationProvider(userDetailsService,passwordEncoder))
                .addFilterBefore(jwtTokenGeneratorFilter, UsernamePasswordAuthenticationFilter.class)
                // 访问控制时登录状态检查过滤器
                .addFilterBefore(new ChcitTokenAuthenticationFilter(authenticationManager,securityProperties,userDetailsService), ChcitTokenGeneratorFilter.class);
        ;
    }
}
